package com.authine.cloudpivot.ext.domain.impl;

import com.authine.cloudpivot.ext.util.MailUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.concurrent.LinkedTransferQueue;

/**
 * @program: gm-market
 * @description:
 * @author: wlw
 * @create: 2021-09-02 15:33
 **/
@Slf4j
public class SendMailTask extends Thread {
    private  static LinkedTransferQueue<MailTask> queue = new LinkedTransferQueue<>();
    private static SendMailTask sendMailTask;

    static{
        sendMailTask = new SendMailTask();
        sendMailTask.start();
    }

    @Override
    @SuppressWarnings("all")
    public void run() {

        while (true) {
            try {
                log.info("---------------------run--------------------");
                MailTask mailTask = queue.take();
                MailUtil.send(mailTask);
            } catch (InterruptedException e) {
                log.error("发送邮件，执行异常",e);
            }
        }

    }

    public static boolean add(MailTask mailTask){
        if(Objects.isNull(mailTask)){
            return false;
        }
        return queue.add(mailTask);
    }
}
